package com.higold.cloud.ui.activity

import android.widget.ImageView
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.gyf.immersionbar.ktx.immersionBar
import com.higold.cloud.MyApplication
import com.higold.cloud.R
import com.higold.cloud.base.BaseActivity
import com.higold.cloud.glide.GlideApp
import com.higold.cloud.mvp.contract.ShopDetailContract
import com.higold.cloud.mvp.model.bean.shopDeatail.ShopDetail
import com.higold.cloud.mvp.presenter.ShopDetailPresenter
import com.higold.cloud.net.exception.ErrorStatus
import com.higold.cloud.showToast
import com.higold.cloud.utils.AppUtils
import com.higold.cloud.utils.CircleTransform
import kotlinx.android.synthetic.main.activity_shop_detail.*

/**
 *    author : lxz
 *    date   : 2019/10/12 15:02
 *    desc   :
 */
class ShopDetailActivity : BaseActivity(), ShopDetailContract.View {

    private val mPresenter by lazy { ShopDetailPresenter() }
    private var shopId: Int = 0

    override fun layoutId(): Int = R.layout.activity_shop_detail

    override fun initData() {
        shopId = intent.getIntExtra("shopId", 0)
    }

    override fun initView() {
        mPresenter.attachView(this)
        immersionBar {
            statusBarDarkFont(true)
            navigationBarDarkIcon(true)
            fitsSystemWindows(true)
        }
    }

    override fun start() {
        mPresenter.getShopDetail(shopId)
    }

    override fun setShopDetail(shopDetail: ShopDetail) {
        multipleStatusView.showContent()
        setData(shopDetail)
    }

    override fun showError(errorMsg: String, errorCode: Int) {
        showToast(errorMsg)
        if (errorCode == ErrorStatus.NETWORK_ERROR) {
            multipleStatusView.showNoNetwork()
        } else {
            multipleStatusView.showError()
        }
    }

    override fun showLoading() {
        multipleStatusView.showLoading()
    }

    override fun dismissLoading() {

    }

    private fun setData(shopDetail: ShopDetail) {

        setImage(iv_shop, shopDetail.data.shop_pic)
        setImage(iv_license, shopDetail.data.business_pic)

        tv_shop_name.text = shopDetail.data.shop_name
        tv_holder_name.text = shopDetail.data.contact
        tv_phone.text = shopDetail.data.mobile
        tv_address.text = shopDetail.data.city
        tv_address_detail.text = shopDetail.data.address
        tv_dev_time.text = AppUtils.getDateString(shopDetail.data.createtime, "yyyy-MM-dd HH:mm:ss")
        tv_remarks.text = shopDetail.data.remark
        tv_license_num.text = shopDetail.data.business_no
        tv_manager.text = shopDetail.data.saleman_name
        tv_job.text = shopDetail.data.saleman_shortname
    }

    private fun setImage(iv: ImageView, url: String) {
        GlideApp.with(MyApplication.context)
                .load(url)
                .placeholder(R.drawable.ic_load_p)
                .skipMemoryCache(true)
                .centerCrop()
                .transition(DrawableTransitionOptions().crossFade())
                .thumbnail(0.5f)
                .into(iv)
    }

}